﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EasyDesktop.Model;
using System.Xml;
using System.Windows.Controls;

namespace EasyDesktop.XmlEditor
{
    class XmlReader
    {
        public XmlReader()
        {

        }

        public List<MalveButton> GetButtonList()
        {
            List<MalveButton> malveButtonList = new List<MalveButton>();

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("Easy.xml");
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("Easy").ChildNodes;

            foreach (XmlNode xn in nodeList)//遍历所有子节点
            {
                MalveButton button = new MalveButton();

                XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型
                foreach (XmlNode item in xe)
                {
                    if(item.Name == "Name") button.Name = item.InnerText.Replace("@", "=").Replace("!", "&");
                    if (item.Name == "Url") button.Url = item.InnerText.Replace("@", "=").Replace("!", "&");
                }

                button.Button.Click += Button_Click;

                malveButtonList.Add(button);
            }

            return malveButtonList;
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Button button = sender as Button;

            List<MalveButton> malveButtonList = GetButtonList();

            for (int i = 0; i < malveButtonList.Count; i++)
            {
                if(malveButtonList[i].Name == button.Content.ToString())
                {
                    System.Diagnostics.Process.Start(malveButtonList[i].Url);
                    break;
                }
            }
            
        }
    }
}
